package com.ptkc.ext;

import com.ptkc.core.canal.handler.EventTypeHandler;
import com.ptkc.core.esearch.model.DeliverCostTimeslot;
import com.ptkc.core.esearch.repos.DeliverCostRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author: Mr.Jiao
 * @date: 2018年12月15日
 */
@Component
@Slf4j
public class DeliverCostTimeslotCanalHandler extends EventTypeHandler<DeliverCostTimeslot> {

    @Autowired
    private DeliverCostRepository deliverCostRepository;

    public DeliverCostTimeslotCanalHandler() {
        super("runfast", "runfast_deliver_cost_timeslot");
    }

    @Override
    public Class<? extends DeliverCostTimeslot> getEntryClass() {
        return DeliverCostTimeslot.class;
    }

    @Override
    public void onUpdate(DeliverCostTimeslot rowData) {
        if(rowData != null && rowData.getId() != null) {
            try {
                deliverCostRepository.saveTimeCost(rowData);
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }

    @Override
    public void onInsert(DeliverCostTimeslot rowData) {
        this.onUpdate(rowData);
    }

    @Override
    public void onDelete(DeliverCostTimeslot rowData) {
        try {
            deliverCostRepository.delTimeCost(rowData);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
}
